package me.youline.dataServer.service.image;

public class ImageSize {

	public final int height;
	
	public final int width;

	private static final String SEPARATE_SIG = "x";
	
	private static BlankImageSize BLANK = new BlankImageSize(0, 0);
	
	private static ThumbnailImageSize THUMB = new ThumbnailImageSize(0, 0);
	
	public ImageSize(int height,int width){
		this.height = height;
		this.width = width;
	}

	public ImageSize(String widthHeightformat){
		int [] size = this.pareseSizeFormat(widthHeightformat);
		this.width = size[0];
		this.height = size[1];
	}
	
	/**
	 * 解析图片尺寸格式
	 * @param widthHeightformat
	 * @return
	 */
	private int[] pareseSizeFormat(String widthHeightformat){
		String [] size = widthHeightformat.split(SEPARATE_SIG);
		if(size.length < 2  || size.length > 2){
			throw new RuntimeException("非法的图片尺寸字符串格式,应该为 宽度"+SEPARATE_SIG+"高度");
		}
		return new int[]{Integer.parseInt(size[0]),Integer.parseInt(size[1])};
	}
	
	
	/**
	 * 获取高度
	 * @return
	 */
	public int getHeight() {
		return height;
	}
	
	/**
	 * 获取宽度
	 * @return
	 */
	public int getWidth() {
		return width;
	}
	
	/**
	 * 判断当前对象是否为空尺寸对象
	 * @return
	 */
	public boolean isBlankSize(){
		if(this instanceof BlankImageSize){
			return true;
		}
		return false;
	}
	
	/**
	 * 获取空图像尺寸对象
	 * @return
	 */
	public static ImageSize createBlankImageSize(){
		return BLANK;
	}
	
	public static ImageSize createThumbnailImageSize(){
		return THUMB;
	}

	/**
	 * 获取尺寸格式字符串
	 * @return
	 */
	public String getSizeFormatStr(){
		return this.width+SEPARATE_SIG+this.height;
	}
	
	
	public static interface SpecialImageSize{
		
		
	}
	
	/**
	 * 空图片尺寸格式，此格式表示图片不做处理，即标识为原图
	 * @author linhan
	 *
	 */
	private static class BlankImageSize extends ImageSize implements SpecialImageSize{

		public BlankImageSize(int height, int width) {
			super(height, width);
		}

		@Override
		public int getHeight() {
			return 0;
		}

		@Override
		public int getWidth() {
			return 0;
		}

		@Override
		public String getSizeFormatStr() {
			return "";
		}
	}
	
	private static class ThumbnailImageSize extends ImageSize  {
		
		public ThumbnailImageSize(int height, int width) {
			super(height, width);
		}

		@Override
		public String getSizeFormatStr() {
			return "thumbnail";
		}
	}
	
}
